using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Compiled;

namespace OR.Lazarus.Library.DataTier.Compiled.Lazarus
{
	/// <summary>
	/// Defines a Data Table within a Business View, bound to a single data entity.
	/// </summary>
   public class DataTableDetail : EntityBase
   {
		protected System.String mName;
		protected string mRef;
		protected string mType;
		protected string mDescription;
	
		protected Derived.DataColumnCollection mColumns;

		public DataTableDetail()
		{
			mColumns = new Derived.DataColumnCollection();
			mColumns.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Ref
		{
			get
			{
				return ( mRef );
			}
			set
			{
				mRef = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Type
		{
			get
			{
				return ( mType );
			}
			set
			{
				mType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.DataColumnCollection Columns
		{
			get
			{
				return ( mColumns );
			}
			set
			{
				mColumns = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mRef = DataUtility.GetString( pSource, "ref" );
			mType = DataUtility.GetString( pSource, "type" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzc:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vColumns = ( XmlElement )pSource.SelectSingleNode( "lzc:columns", PersistenceConfig.Namespaces );
			
			if ( vColumns != null )
			{
				mColumns.LoadXml( vColumns );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzc", "datatable", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mRef ) )
			{
				vNewElement.SetAttribute( "ref", mRef );
			}
					
			if ( !String.IsNullOrEmpty( mType ) )
			{
				vNewElement.SetAttribute( "type", mType );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzc", "description", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mColumns.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.DataTableDetail v_oReturn = new Derived.DataTableDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Ref = mRef;
			v_oReturn.Type = mType;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.DataColumnDetail vDetail in mColumns )
			{
				v_oReturn.Columns.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.DataTableDetail CloneTyped()
		{
			return ( ( Derived.DataTableDetail )this.Clone() );
		}
   }
}
   